package com.windowes.weichang.controller.callback;

import com.google.gson.Gson;
import com.windowes.weichang.dto.EBPayRespDTO;
import com.windowes.weichang.service.DemandService;
import com.windowes.weichang.utils.pay.ZGTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/8/1 下午8:01
 * @description:
 *
 **/
@RestController
@RequestMapping("pay")
@Slf4j
public class PayCallbackController {


    @Autowired
    DemandService demandService;


    @GetMapping("createProjectCallback")
    public Object callback(HttpServletRequest request) {
        String data = request.getParameter("data");
        Map<String, String> stringStringMap = ZGTUtils.decryptData(data);

        Gson gson = new Gson();
        String toJson = gson.toJson(stringStringMap);
        log.info("支付结果:" + toJson);
        EBPayRespDTO ebPayRespDTO = gson.fromJson(toJson, EBPayRespDTO.class);
        demandService.confirmProjectAndPayCallback(ebPayRespDTO);
        return "SUCCESS";
    }

    @GetMapping("completeAllCallback")
    public Object completeAll(HttpServletRequest request) {
        String data = request.getParameter("data");
        Map<String, String> stringStringMap = ZGTUtils.decryptData(data);
        Gson gson = new Gson();
        String toJson = gson.toJson(stringStringMap);
        log.info("completeAll支付结果:" + toJson);
        EBPayRespDTO ebPayRespDTO = gson.fromJson(toJson, EBPayRespDTO.class);
        demandService.completeAllCallback(ebPayRespDTO);
        return "SUCCESS";
    }

    @GetMapping("")
    public Object doPost(HttpServletRequest request) {
        //获取各个请求参数
        String requestid = formatStr(request.getParameter("requestid"));
        String amount = formatStr(request.getParameter("amount"));
        String assure = formatStr(request.getParameter("assure"));
        String productname = formatStr(request.getParameter("productname"));
        String productcat = formatStr(request.getParameter("productcat"));
        String productdesc = formatStr(request.getParameter("productdesc"));
        String divideinfo = formatStr(request.getParameter("divideinfo"));
        String callbackurl = formatStr(request.getParameter("callbackurl"));
        String webcallbackurl = formatStr(request.getParameter("webcallbackurl"));
        String bankid = formatStr(request.getParameter("bankid"));
        String period = formatStr(request.getParameter("period"));
        String memo = formatStr(request.getParameter("memo"));
        String orderexpdate = formatStr(request.getParameter("orderexpdate"));
        String payeename = formatStr(request.getParameter("payeename"));
        String payproducttype = formatStr(request.getParameter("payproducttype"));
        String userno = formatStr(request.getParameter("userno"));
        String ip = formatStr(request.getParameter("ip"));
        String cardname = formatStr(request.getParameter("cardname"));
        String idcard = formatStr(request.getParameter("idcard"));
        String bankcardnum = formatStr(request.getParameter("bankcardnum"));
        String mobilephone = formatStr(request.getParameter("mobilephone"));
        String appid = formatStr(request.getParameter("appid"));
        String openid = formatStr(request.getParameter("openid"));
        String directcode = formatStr(request.getParameter("directcode"));
        String platform = formatStr(request.getParameter("platform"));
        String appname = formatStr(request.getParameter("appname"));
        String appstatement = formatStr(request.getParameter("appstatement"));
        Map<String, String> params = new HashMap<String, String>();
        params.put("requestid", requestid);
        params.put("amount", amount);
        params.put("assure", assure);
        params.put("productname", productname);
        params.put("productcat", productcat);
        params.put("productdesc", productdesc);
        params.put("divideinfo", divideinfo);
        params.put("callbackurl", callbackurl);
        params.put("webcallbackurl", webcallbackurl);
        params.put("bankid", bankid);
        params.put("period", period);
        params.put("memo", memo);
        params.put("orderexpdate", orderexpdate);
        params.put("payeename", payeename);
        params.put("payproducttype", payproducttype);
        params.put("userno", userno);
        params.put("ip", ip);
        params.put("cardname", cardname);
        params.put("idcard", idcard);
        params.put("bankcardnum", bankcardnum);
        params.put("mobilephone", mobilephone);
        params.put("appid", appid);
        params.put("openid", openid);
        params.put("directcode", directcode);
        params.put("platform", platform);
        params.put("appname", appname);
        params.put("appstatement", appstatement);

        System.out.println("params====" + params);
        //第一步 生成密文data
        String data = ZGTUtils.buildData(params, ZGTUtils.PAYAPI_REQUEST_HMAC_ORDER);

        //第二步 发起请求
        String requestUrl = ZGTUtils.getRequestUrl(ZGTUtils.PAYAPI_NAME);
        Map<String, String> responseMap = ZGTUtils.httpPost(requestUrl, data);

        System.out.println(requestUrl + "?customernumber=" + ZGTUtils.getCustomernumber() + "&data=" + data);

        //第三步 判断请求是否成功，
        if (responseMap.containsKey("code")) {
            return responseMap;
        }

        //第四步 解密同步响应密文data，获取明文参数
        String responseData = responseMap.get("data");
        Map<String, String> responseDataMap = ZGTUtils.decryptData(responseData);

        System.out.println("易宝的同步响应：" + responseMap);
        System.out.println("data解密后明文：" + responseDataMap);

        //第五步 code=1时，方表示接口处理成功
        if (!"1".equals(responseDataMap.get("code"))) {
            System.out.println("code = " + responseDataMap.get("code") + "<br>");
            System.out.println("msg  = " + responseDataMap.get("msg"));
            return responseDataMap;
        }

        //第六步 hmac签名验证
        if (!ZGTUtils.checkHmac(responseDataMap, ZGTUtils.PAYAPI_RESPONSE_HMAC_ORDER)) {
            return "<br>hmac check error!<br>";
        }

        //第七步 进行业务处理
        //当支付方式为微信扫码，展示二维码
        return responseDataMap;
    }


    public String formatStr(String text) {
        return text == null ? "" : text.trim();
    }
}